<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\model\GoodsType;
use app\admin\model\Brand as brands;
use app\admin\model\BrandType;
use Db;

class Brand extends Controller
{   
    protected $middleware = [
        'Before'=> ['only' 	=> ['index'] ],
    ];
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {   
        $list = brands::paginate(5);
        // 渲染模板输出
        return view('',['list'=>$list]);
    }

    //根据三级分类id取出品牌
    public function ajax_get_brand()
    {
        $id = (int)$_GET['id'];
        
        $data = BrandType::where('type_id',$id)->alias('bt')->join('brand b','bt.brand_id = b.id')->select();
        return json($data);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {   
        $model = new GoodsType;
        $data = $model->topCat();

        return $this->fetch('brand-add',['data'=>$data]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $req)  
    {       
        $file = request()->file('file');
        // var_dump($file);
       
        $url = env('root_path');
        $path = 'public/static/uploads/admin/brand/';
        $info = $file->move($url.$path);
        // echo '<pre>';
        // var_dump($info->getSaveName());die;
        if($info){

            $image = \think\Image::open($url. $path.$info->getSaveName());  
            $image->thumb(150, 75)->save($url.$path.$info->getSaveName());
            $brand_path = '/static/uploads/admin/brand/'.$info->getSaveName();
            // die;
            $data = ['brand_name'=>$req->brand_name,'brand_logo'=>$brand_path];
            $brand_id = Db::table('brand')->strict(false)->insertGetId($data);

            $data = ['type_id'=>$req->type3_id,'brand_id'=> $brand_id];
            Db::table('brand_type')->strict(false)->insert( $data);

            return redirect("/admin/brand/create")->with('status','1');

        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }    

    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {   
        // dump($id);
        $data['data'] = brands::where('id',$id)->find();

        return view('brand-edit',$data);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {   
        $file = request()->file('file');

        $url = env('root_path');
        $path = 'public/static/uploads/admin/brand/';
        $info = $file->move($url.$path);

        if($info){
            $image = \think\Image::open($url. $path.$info->getSaveName());  
            $image->thumb(150, 75)->save($url.$path.$info->getSaveName());
            $brand_path = '/static/uploads/admin/brand/'.$info->getSaveName();
        }

        $brand = brands::get($id);
        unlink($url.'public'.$brand->brand_logo);
        $brand->brand_logo = $brand_path;
        $brand->brand_name = $request->brand_name;
        $brand->save();
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {

        brands::destroy($id);

    }
}
